Write-Host "=== Testing approval data changes ===" -ForegroundColor Green

$baseUrl = "http://localhost:8080"

# Step 1: Login to get token
Write-Host "`n1. Login to get token..." -ForegroundColor Cyan
$loginData = @{
    userName = "admin"
    password = "1q2w3E*"
} | ConvertTo-Json

try {
    $loginResponse = Invoke-RestMethod -Uri "$baseUrl/api/app/user-login/login" -Method POST -Body $loginData -Headers @{"Content-Type"="application/json"}
    $token = $loginResponse.token
    Write-Host "Login successful, got token" -ForegroundColor Green
}
catch {
    Write-Host "Login failed: $($_.Exception.Message)" -ForegroundColor Red
    exit
}

# Step 2: Get workflow instances (before approval)
Write-Host "`n2. Get workflow instances (before approval)..." -ForegroundColor Cyan
try {
    $instancesResponse = Invoke-RestMethod -Uri "$baseUrl/api/app/workflow/instances" -Method GET -Headers @{"Authorization"="Bearer $token"; "Content-Type"="application/json"}
    
    if ($instancesResponse.code -eq 200 -and $instancesResponse.data.Count -gt 0) {
        $workflowInstance = $instancesResponse.data[0]
        $workflowInstanceId = $workflowInstance.workflowInstanceId
        Write-Host "Found workflow instance: $workflowInstanceId" -ForegroundColor Green
        Write-Host "   Status: $($workflowInstance.status)" -ForegroundColor Yellow
        Write-Host "   Current Step: $($workflowInstance.currentStep)" -ForegroundColor Yellow
        Write-Host "   Approval Records Count: $($workflowInstance.approvalRecords.Count)" -ForegroundColor Yellow
    }
    else {
        Write-Host "No workflow instances found" -ForegroundColor Red
        exit
    }
}
catch {
    Write-Host "Failed to get workflow instances: $($_.Exception.Message)" -ForegroundColor Red
    exit
}

# Step 3: Execute approval
Write-Host "`n3. Execute approval..." -ForegroundColor Cyan
$approvalData = @{
    workflowInstanceId = $workflowInstanceId
    stepName = "ManagerApproval"
    approverId = "3a1b6655-5c0b-92ba-3d8b-aeb17daa1e53"
    approverName = "Warehouse Manager"
    approvalResult = "Approved"
    approvalComment = "Test approval passed"
} | ConvertTo-Json

try {
    $approvalResponse = Invoke-RestMethod -Uri "$baseUrl/api/app/workflow/approve" -Method POST -Body $approvalData -Headers @{"Authorization"="Bearer $token"; "Content-Type"="application/json"}
    
    if ($approvalResponse.code -eq 200) {
        Write-Host "Approval executed successfully" -ForegroundColor Green
    }
    else {
        Write-Host "Approval execution failed: $($approvalResponse.message)" -ForegroundColor Red
    }
}
catch {
    Write-Host "Approval execution exception: $($_.Exception.Message)" -ForegroundColor Red
}

# Wait for data update
Start-Sleep -Seconds 2

# Step 4: Get workflow instances (after approval)
Write-Host "`n4. Get workflow instances (after approval)..." -ForegroundColor Cyan
try {
    $instancesResponseAfter = Invoke-RestMethod -Uri "$baseUrl/api/app/workflow/instances" -Method GET -Headers @{"Authorization"="Bearer $token"; "Content-Type"="application/json"}
    
    if ($instancesResponseAfter.code -eq 200 -and $instancesResponseAfter.data.Count -gt 0) {
        $workflowInstanceAfter = $instancesResponseAfter.data[0]
        Write-Host "Workflow instance info after approval:" -ForegroundColor Green
        Write-Host "   Status: $($workflowInstanceAfter.status)" -ForegroundColor Yellow
        Write-Host "   Current Step: $($workflowInstanceAfter.currentStep)" -ForegroundColor Yellow
        Write-Host "   Approval Records Count: $($workflowInstanceAfter.approvalRecords.Count)" -ForegroundColor Yellow
        
        # Show approval record details
        if ($workflowInstanceAfter.approvalRecords.Count -gt 0) {
            Write-Host "`n   Approval record details:" -ForegroundColor Cyan
            foreach ($record in $workflowInstanceAfter.approvalRecords) {
                Write-Host "    - Step: $($record.stepName)" -ForegroundColor White
                Write-Host "      Approver: $($record.approverName)" -ForegroundColor White
                Write-Host "      Result: $($record.approvalResult)" -ForegroundColor White
                Write-Host "      Comment: $($record.approvalComment)" -ForegroundColor White
                Write-Host "      Time: $($record.approvalTime)" -ForegroundColor White
                Write-Host ""
            }
        }
    }
    else {
        Write-Host "Failed to get workflow instances after approval" -ForegroundColor Red
    }
}
catch {
    Write-Host "Exception getting workflow instances after approval: $($_.Exception.Message)" -ForegroundColor Red
}

# Step 5: Query workflow status
Write-Host "`n5. Query workflow status..." -ForegroundColor Cyan
try {
    $statusResponse = Invoke-RestMethod -Uri "$baseUrl/api/app/workflow/status/$workflowInstanceId" -Method GET -Headers @{"Authorization"="Bearer $token"; "Content-Type"="application/json"}
    
    if ($statusResponse.code -eq 200) {
        Write-Host "Workflow status query successful" -ForegroundColor Green
        Write-Host "   Status data: $($statusResponse.data | ConvertTo-Json -Depth 3)" -ForegroundColor Yellow
    }
    else {
        Write-Host "Workflow status query failed: $($statusResponse.message)" -ForegroundColor Red
    }
}
catch {
    Write-Host "Workflow status query exception: $($_.Exception.Message)" -ForegroundColor Red
}

Write-Host "`n=== Test completed ===" -ForegroundColor Green 